<template>
  <div>
    <el-dialog
      v-toggle-full
      v-loading.lock="loading"
      class="base-dialog order-handle"
      :custom-class="'panna-dialog_big'"
      :append-to-body="true"
      title="确认入库"
      align="center"
      :close-on-click-modal="false"
      :visible.sync="innerDialogFormVisible"
      @closed="closeDialog"
    >
      <!-- 基础信息 -->
      <el-divider class="el-divider-specail" content-position="left"
        >基础信息</el-divider
      >
      <el-form ref="formData" :model="formData" label-width="120px" status-icon>
        <el-row>
          <el-col :span="12">
            <el-form-item label="入库单号">
              {{ formData.orderNum }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库仓库">
              {{ formData.storageName }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人">
              {{ formData.applicantName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间">
              {{ formData.createTime }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="业务类型">
              {{ formData.orderTypeName }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理人">
              <el-select
                v-model="formData.conductorId"
                style="width: 50%"
                placeholder="请选择"
                filterable
                remote
                reserve-keyword
                :remote-method="remoteUser"
                @change="conductorChange"
              >
                <el-option
                  v-for="item in userList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请原因">
              {{ formData.remark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 已检清单 -->
      <el-divider
        class="el-divider-specail el-divider-top"
        content-position="left"
        >已检清单</el-divider
      >
      <el-table
        v-if="formData.inspectionDetails.length > 0"
        :data="formData.inspectionDetails"
        :header-cell-style="{ height: '50px' }"
        :cell-style="{ height: '44px', overflow: 'auto' }"
        style="width: 100%; height: auto; overflow: auto"
      >
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="检验单号">
          <template slot-scope="scope">
            {{ scope.row.formNum }}
          </template>
        </el-table-column>
        <el-table-column label="物料编号">
          <template slot-scope="scope">
            {{ scope.row.productNum }}
          </template>
        </el-table-column>
        <el-table-column label="物料名称">
          <template slot-scope="scope">
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="规格型号">
          <template slot-scope="scope">
            {{ scope.row.size }}
          </template>
        </el-table-column>
        <el-table-column label="供应商">
          <template slot-scope="scope">
            {{ scope.row.supplierName }}
          </template>
        </el-table-column>
        <el-table-column label="存储条件">
          <template slot-scope="scope">
            {{ scope.row.storeCondition }}
          </template>
        </el-table-column>
        <el-table-column label="包装单位">
          <template slot-scope="scope">
            {{ scope.row.packingUnit }}
          </template>
        </el-table-column>
        <el-table-column label="合格数量">
          <template slot-scope="scope">
            {{ scope.row.qualifiedCount }}
          </template>
        </el-table-column>
        <el-table-column label="实收数量">
          <template slot-scope="scope">
            {{ scope.row.actualCount }}
          </template>
        </el-table-column>
        <el-table-column label="待收数量">
          <template slot-scope="scope">
            {{
              parseInt(scope.row.pendingCount) > 0
                ? scope.row.pendingCount
                : "已完成"
            }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div v-if="scope.row.pendingCount > 0" class="base-table-operation">
              <el-link
                size="mini"
                type="primary"
                @click="openInputProductDetailsDialog(scope.row)"
                >确认入库</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 入库明细 -->
      <el-divider
        class="el-divider-specail el-divider-top"
        content-position="left"
        >入库明细</el-divider
      >
      <el-table
        :data="formData.productDetails"
        :header-cell-style="{ height: '50px' }"
        :cell-style="{ height: '44px', overflow: 'auto' }"
        style="width: 100%; height: auto; overflow: auto"
      >
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column label="物料编号">
          <template slot-scope="scope">
            {{ scope.row.productNum }}
          </template>
        </el-table-column>
        <el-table-column label="物料名称">
          <template slot-scope="scope">
            {{ scope.row.productName }}
          </template>
        </el-table-column>
        <el-table-column label="规格型号">
          <template slot-scope="scope">
            {{ scope.row.size }}
          </template>
        </el-table-column>
        <el-table-column label="供应商">
          <template slot-scope="scope">
            {{ scope.row.supplierName }}
          </template>
        </el-table-column>
        <el-table-column label="证书编号">
          <template slot-scope="scope">
            {{ scope.row.certificateNum }}
          </template>
        </el-table-column>
        <el-table-column label="气瓶编号">
          <template slot-scope="scope">
            {{ scope.row.sysBusinessAirBottleNum }}
          </template>
        </el-table-column>
        <el-table-column label="生产日期">
          <template slot-scope="scope">
            {{ formatTime(scope.row.dateManufacture) }}
          </template>
        </el-table-column>
        <el-table-column label="入库数量">
          <template slot-scope="scope">
            {{ parseInt(scope.row.productCount) }}
          </template>
        </el-table-column>
        <el-table-column label="入库库区">
          <template slot-scope="scope">
            {{ scope.row.storageAreaName }}
          </template>
        </el-table-column>
        <el-table-column label="备注">
          <template slot-scope="scope">
            {{ scope.row.remark }}
          </template>
        </el-table-column>
        <el-table-column label="操作人">
          <template slot-scope="scope">
            {{ scope.row.operatorName }}
          </template>
        </el-table-column>
        <el-table-column label="操作时间">
          <template slot-scope="scope">
            {{ scope.row.createTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="base-table-operation">
              <el-link
                size="mini"
                type="primary"
                @click="deleteProductDetails(scope.row)"
                >删除</el-link
              >
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 录入入库数据 Dialog -->
      <InputProductDetailsDialog
        v-if="dialogInputDetailsVisible"
        ref="InputProductDetailsDialog"
        :dialog-input-details-visible.sync="dialogInputDetailsVisible"
        :row-data="rowData"
        @confirmAfter="confirmAfter"
      >
      </InputProductDetailsDialog>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button @click="cancel">返 回</el-button>
        <el-button type="primary" @click="onSubmit('formData')"
          >处理完成</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getInfo, complete } from "../../../../api/Material/MaterialOrderIn.js";
import { deleteRecord } from "../../../../api/Material/MaterialOrderProductDetails.js";
import InputProductDetailsDialog from "./RKInputProductDetailsDialog.vue";
import { formatTime } from "../../../../utils/time";
import {getUserListNoPage} from "@/ModulePermission/api/Permission/PermissionUser.js";

export default {
  name: "RKOrderHandle",
  components: { InputProductDetailsDialog },
  props: {
    dialogHandleVisible: {
      type: Boolean,
      default: false,
    },
    editId: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      loading: false,
      innerDialogFormVisible: false,
      dialogInputDetailsVisible: false,
      formLabelWidth: "100px",
      conductorId: "",
      userList: [],
      initData: {
        orderId: 0,
        orderNum: "",
        formTypeCode: 0,
        formTypeName: "",
        orderTypeCode: 0,
        orderTypeName: "",
        conductorId: "",
        storageId: 0,
        storageName: "",
        remark: "",
        applicantId: null,
        applicantName: null,
        inspectionDetails: [],
        productDetails: [],
      },
      formData: {
        orderId: 0,
        orderNum: "",
        formTypeCode: 0,
        formTypeName: "",
        orderTypeCode: 0,
        orderTypeName: "",
        conductorId: "",
        storageId: 0,
        storageName: "",
        remark: "",
        applicantId: null,
        applicantName: null,
        createTime: null,
        inspectionDetails: [],
        productDetails: [],
      },
    };
  },
  watch: {},
  created() {
    this.innerDialogFormVisible = this.dialogHandleVisible;
    this.init();
  },
  methods: {
    conductorChange(val) {
      this.conductorId = val;
    },
    formatTime,
    async init() {
      await this.getUserData();
      await this.getDetail();
    },
    getUserData() {
     getUserListNoPage({ userStatus: 1 })
        .then((res) => {
          this.userList = res.data;
        });
    },
    remoteUser(query) {
      getUserListNoPage({ name: query, userStatus: 1 })
        .then((res) => {
          this.userList = res.data;
        });
    },
    getDetail() {
      getInfo({ id: this.editId }).then((res) => {
        this.formData = res.data;
        if (this.conductorId) {
          this.formData.conductorId = this.conductorId;
        } else {
          this.formData.conductorId = this.$store.getters.currentUserId;
        }
        if (res.data && this.formData.inspectionDetails?.length > 0) {
          this.formData.orderId = this.formData.id;
          this.formData.inspectionDetails = res.data.inspectionDetails;
          this.formData.productDetails = res.data.productDetails;
        }
      });
    },
    // 录入入库数据对话框
    openInputProductDetailsDialog(val) {
      this.rowData = {
        handleId: val.id,
        orderId: val.orderId,
        productId: val.productId,
        productNum: val.productNum,
        productName: val.name,
        size: val.size,
        packingUnit: val.packingUnit,
        pendingCount: val.pendingCount,
        storageId: this.formData.storageId,
      };
      this.dialogInputDetailsVisible = true;
    },
    // 确定入库数据(回调)
    confirmAfter() {
      this.getDetail();
    },
    deleteProductDetails(rowData) {
      this.Helper.deleteConfirm(() => {
        this.loading = true;
        deleteRecord({ id: rowData.id })
          .then((res) => {
            this.getDetail();
            this.loading = false;
          })
          .catch(() => {
            this.loading = false;
          });
      });
    },
    // 检验完成
    onSubmit() {
      this.$refs.formData.validate((valid) => {
        if (valid) {
          if (this.formData.inspectionDetails?.length <= 0) {
            this.$message.error("已检清单无数据，无需处理");
            return false;
          }
          if (
            this.formData.conductorId === null ||
            this.formData.conductorId === "" ||
            this.formData.conductorId === 0
          ) {
            this.$message.error("请选择处理人");
            return false;
          }
          const unfinished = this.formData.inspectionDetails.find(
            (item) =>
              parseInt(item.qualifiedCount) !== parseInt(item.actualCount)
          );
          if (unfinished) {
            this.$message.error("已检清单未全部入库");
            return false;
          }
          this.loading = true;
          this.errorMsg = this.Helper.deepCopy(this.initErrorMsg);
          if (this.formData.orderId) {
            complete(this.formData)
              .then((res) => {
                if (res.code === 200) {
                  this.closeDialog();
                } else {
                  this.showValidateMsgCheck(res);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            this.loading = false;
            this.$message.error("参数错误");
            return false;
          }
        } else {
          return false;
        }
      });
    },
    cancel() {
      this.innerDialogFormVisible = false;
      this.$emit("update:dialogHandleVisible", false);
      this.$refs["formData"].resetFields();
      this.$emit("update:editId", "");
    },
    closeDialog() {
      this.$parent.getList();
      this.cancel();
    },
  },
};
</script>
<style lang="scss">
.order-handle {
  margin-top: 10px;
  width: 100%;

  .el-dialog__footer .el-button {
    width: 90px;
  }

  .el-table .cell {
    overflow: visible;
  }

  .el-form {
    border: 1px solid #e6ebf5;

    .el-row {
      border-bottom: 1px solid #e6ebf5;

      &:last-child {
        border-bottom: none;
      }

      .el-col {
        &:nth-child(2n + 1) {
          .el-form-item {
            .el-form-item__label {
              border-left: none;
            }
          }
        }

        .el-form-item {
          margin: 0;
          height: 40px;
          line-height: 40px;

          .el-form-item__label {
            height: 100%;
            line-height: 40px;
            background: #f1f8ff;
            border-left: 1px solid #e6ebf5;
            border-right: 1px solid #e6ebf5;
            text-align: center;
            color: #909399;
            font-weight: bold;
          }

          .el-form-item__content {
            text-align: left;
            height: 100%;
            line-height: 40px;
            margin-left: 20px;
          }
        }
      }
    }
  }

  .title {
    display: inline-block;
    vertical-align: middle;
  }

  .el-divider-specail {
    background: transparent;

    .el-divider__text {
      font-weight: bold;
      font-size: 15px;
      left: -20px;
    }

    &.el-divider-top {
      margin-top: 30px;
    }
  }

  .button-area {
    display: inline-block;
    vertical-align: middle;
    margin-left: 20px;
  }
}
</style>
